package com.xyd.Servlet;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xyd.Service.NewsService;
import com.xyd.bean.News;
import com.xyd.dao.NewsMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet({"/ListNews", "/InsertNews", "/UpdateNews", "/DeleteNews","/ModifyNews","/detailNews"})
public class NewsServlet extends HttpServlet {
    NewsService newsService = new NewsService();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        if ("/ListNews".equals(path)) {
            doListNews(request, response);
        } else if ("/InsertNews".equals(path)) {
            doInsertNews(request, response);
        } else if ("/UpdateNews".equals(path)) {
            doUpdateNews(request, response);
        } else if ("/DeleteNews".equals(path)) {
            doDeleteNews(request, response);
        }else if ("/ModifyNews".equals(path)){
            doModifyNews(request,response);
        }else if ("/detailNews".equals(path)){
            doDetailNews(request,response);
        }
    }

    public void doListNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        List<News> list = newsService.getPageNews();
        request.setAttribute("newsList",list);
        request.getRequestDispatcher("/manage/news.jsp").forward(request, response);
    }

    public void doInsertNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        newsService.insertNews(title, content);
        request.getRequestDispatcher("/ListNews").forward(request, response);
    }

    public void doUpdateNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        int id = Integer.parseInt(request.getParameter("id"));
        News news = newsService.getNewsById(id);
        request.setAttribute("news",news);
        request.getRequestDispatcher("/manage/news-modify.jsp").forward(request, response);
    }

    public void doModifyNews(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        int id = Integer.parseInt(request.getParameter("id"));
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        News news = new News(id,title,content);
        newsService.updateNews(news);
        request.getRequestDispatcher("/ListNews").forward(request,response);
    }
    public void doDeleteNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        int id = Integer.parseInt(request.getParameter("id"));
        newsService.deleteNews(id);
        request.getRequestDispatcher("/ListNews").forward(request,response);
    }
    public void doDetailNews(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        int id = Integer.parseInt(request.getParameter("id"));
        News news = newsService.getNewsById(id);
        request.setAttribute("news",news);
        request.getRequestDispatcher("/manage/news-detail.jsp").forward(request,response);
    }
}
